Imports System.Data
Imports DAOMain

Partial Class Accounting_CommisionDetails
    Inherits WebPageCommon

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Rate_txt.Attributes.Add("onchange", "setAmount()")
        Amount_txt.Attributes.Add("onchange", "setRate()")
        RecievableRate_txt.Attributes.Add("onchange", "setRecAmt()")
        RecievableAmt_txt.Attributes.Add("onchange", "setRecRate()")
        ConversionRate_txt.Attributes.Add("onchange", "setConvAmt()")
        CommAmount_txt.Attributes.Add("onkeypress", "return false;")

        Dim cf As New CIMSfunc

        cf.setNumericFormat(Amount_txt)
        'cf.setNumericFormat(CommAmount_txt)
        cf.setNumericFormat(ConversionRate_txt)

        cf = Nothing

        If Not Page.IsPostBack Then
            PopulateDropDown()
            InitVars()
            LoadDetails()
        End If

        Call SetPageSecurity(divMain)
    End Sub

    Private Sub LoadDetails()
        Dim dao As New VW_COMMISION_PAYABLE_DTL_DAO
        Dim vo As New VW_COMMISION_PAYABLE_DTL_VO
        Dim dt As DataTable
        Dim cf As New CIMSfunc

        If ViewState("CName") <> "&nbsp;" Then
            vo.COM_PAY_SEQ_CD = ViewState("CName")
            dt = dao.Search(vo)
        Else
            vo.PAYMENT_SEQ_CD = ViewState("CID")
            dt = dao.Search(vo)
        End If

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                PaymentNo_txt.Text = .Item("PAYMENT_NO")
                lblPolicyYear.Text = .Item("POLICY_YEAR")
                ARDT_txt.Text = cf.ParseGetDate(.Item("AR_DATE"))
                DueDT_txt.Text = cf.ParseGetDate(.Item("POLICY_DUE_DT"))
                PolicyNo_txt.Text = .Item("POLICY_NO")
                PaymentDT_txt.Text = cf.ParseGetDate(.Item("POLICY_DUE_DT"))
                Advisor_cmb.SelectedValue = .Item("ADVISOR")
                Provider_txt.Text = .Item("PROVIDER_DESC")
                Product_txt.Text = .Item("PRDT_DESC")
                Plan_txt.Text = cf.loadFormat(.Item("PAYMENT_AMT"))
                Rate_txt.Text = .Item("COMM_RATE")
                RecievableAmt_txt.Text = IIf((.Item("com_rec_amt") Is DBNull.Value), "", .Item("com_rec_amt"))
                RecievableRate_txt.Text = IIf((.Item("com_rec_rate") Is DBNull.Value), "", .Item("com_rec_rate")) '.Item("com_rec_rate")
                CommAmount_txt.Text = cf.loadFormat(.Item("CONV_COM_AMT"))
                Amount_txt.Text = cf.loadFormat(.Item("COMM_PAY"))
                Currency_cmb.SelectedValue = .Item("CURRENCY_CD")
                ConvertCurrency_cmb.SelectedValue = .Item("CONV_CURR_CD")
                CommType_cmb.SelectedValue = .Item("COM_TYPE")
                CommStatus_cmb.SelectedValue = .Item("COM_ST")
                ConversionRate_txt.Text = .Item("CONV_RATE")
            End With
        End If

        cf = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Private Sub PopulateDropDown()
        Dim lookup As New LookUp.FillLookUp
        Dim dao As New DAOMain.CommonDAO
        Dim lfunc As New PolyFunc.ASPFunc

        Dim dt As DataTable = dao.ExecQuery("Select user_name, advisor_name From vw_advisor_mstr Order by advisor_name")

        lfunc.BindDropDown(Advisor_cmb, dt, "advisor_name", "user_name")

        With lookup
            .FillDropDown(Currency_cmb, Global.LookUp.FillLookUp.eLookUpType.other, "CURRENCY")
            .FillDropDown(ConvertCurrency_cmb, Global.LookUp.FillLookUp.eLookUpType.other, "CURRENCY")
            .FillDropDown(CommStatus_cmb, Global.LookUp.FillLookUp.eLookUpType.other, "COMMIS_STAT")
            .FillDropDown(CommType_cmb, Global.LookUp.FillLookUp.eLookUpType.other, "COMMIS_TYPE")
        End With

        dao = Nothing
        lfunc = Nothing
        lookup = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New COMMISSION_PAYABLE_DTL_DAO
        Dim vo As New COMMISSION_PAYABLE_DTL_VO
        Dim cf As New CIMSfunc

        With vo
            .ADVISOR_CD = Advisor_cmb.SelectedValue
            .COM_AMT = cf.saveFormat(Amount_txt.Text)
            .CONV_COM_AMT = cf.saveFormat(CommAmount_txt.Text)
            .COM_CURR_CD = Currency_cmb.SelectedValue
            .CONV_CURR_CD = ConvertCurrency_cmb.SelectedValue
            .CONV_RATE = cf.saveFormat(ConversionRate_txt.Text)
            .COM_DT = cf.ParseSetDate(PaymentDT_txt.Text)
            .COM_RATE = cf.saveFormat(Rate_txt.Text)
            .COM_ST = CommStatus_cmb.SelectedValue.ToString
            .COM_TYPE = CommType_cmb.SelectedValue.ToString
            .PAYMENT_SEQ_CD = ViewState("CID")
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .COM_REC_AMT = cf.saveFormat(RecievableAmt_txt.Text)
            .COM_REC_RATE = cf.saveFormat(RecievableRate_txt.Text)

            Select Case ViewState("CName")
                Case "&nbsp;"
                    .COM_PAY_SEQ_CD = cf.GetLastID("COM_PAY_SEQ_CD", "commission_payable_dtl")
                    .COM_CREA_BY = Session("UserName")
                    .COM_CREA_DT = Now

                    dao.Create_Record(vo)
                Case Else
                    .COM_PAY_SEQ_CD = ViewState("CName")
                    .COM_LAST_UPD_BY = Session("UserName")
                    .COM_LAST_UPD_DT = Now

                    dao.Update(vo)
            End Select

            Response.Redirect("~/Accounting/Commision.aspx")
        End With

        cf = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

End Class
